package com.yhy.api.controller;

import cn.hutool.json.JSONUtil;
import com.yhy.api.model.User;
import com.yhy.common.core.model.ContextUser;
import com.yhy.common.core.utils.ContextUtil;
import com.yomahub.tlog.context.TLogContext;
import com.yomahub.tlog.core.context.AspectLogContext;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;
import java.util.concurrent.Executors;

@Api(tags = "测试controller")
@Slf4j
@RestController
@RequestMapping("/test")
@RequiredArgsConstructor
public class TestController {

    @ApiOperation("test接口0")
    @PostMapping("/0")
    public ContextUser test(@RequestBody User user) {
        log.info("test测试日志 :{}", JSONUtil.toJsonStr(user));
        Executors.newSingleThreadExecutor().submit(() -> {
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                log.error("test异步日志");
                e.printStackTrace();
            }
            log.info("test异步日志");
        });
        log.info("test主程日志, TId: {} , TL: {}", TLogContext.getTraceId(), AspectLogContext.getLogValue());
        return ContextUtil.getUser();
    }

    @ApiOperation("test接口1")
    @PostMapping("/1")
    public ContextUser test(@RequestBody Map<String, Object> map) {
        log.info("/test接口1");
        return ContextUtil.getUser();
    }

}
